package org.gd.mongodb;

import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;

import java.net.UnknownHostException;

public abstract class MongoDBFactory {

    private String host;
    private int port;

    private static volatile MongoDBFactory INSTANCE = null;

    public synchronized static MongoDBFactory getInstance() {
        if(INSTANCE == null) {
            synchronized (MongoDBFactory.class) {
                if(INSTANCE == null) {
                    INSTANCE = new MongoDBFactory() {{
                        setHost("localhost");
                        setPort(27017);
                    }};
                }
            }

        }
        return INSTANCE;
    }

    public Mongo getMongo() throws UnknownHostException {
        System.out.println(getHost());
        System.out.println(getPort());
        ServerAddress server = new ServerAddress(host, port);
        MongoOptions options = new MongoOptions();
        return new Mongo(server, options);
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
}
